package com.example.ex3.controller;

import com.example.ex3.dao.UserRepository;
import com.example.ex3.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

@RestController
public class UserController {
    @Autowired
    private UserRepository userRepository;

    @GetMapping("/user/{id}")
    public User findById(@PathVariable Long id){
        return this.userRepository.findById(id).orElse(null);
    }

//    新增数据
    @GetMapping("/saveUser")
    public void saveUser(Long id,String username, String name, Short age, BigDecimal balance){
        User user = new User(id,username,name,age,balance);
        userRepository.save(user);

    }
//    更新数据
    @GetMapping("/updateUser")
    public void updateUser(Long id,String username, String name, Short age, BigDecimal balance){
        User user = new User(id,username,name,age,balance);
        userRepository.save(user);
    }

//    删除数据
    @GetMapping("deleteUser")
    public void deleteUser(Long id){
        userRepository.deleteById(id);
    }
}
